package com.yisoo.ubag.iot.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.iot.model.v20180120.PubRequest;
import com.aliyuncs.iot.model.v20180120.PubResponse;
import com.yisoo.common.to.DevSimpleDataTo;
import com.yisoo.ubag.iot.aliyun.sdk.openapi.AbstractManager;
import org.apache.commons.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.yisoo.ubag.iot.entity.DeviceEntity;
import com.yisoo.ubag.iot.service.DeviceService;
import com.yisoo.common.utils.PageUtils;
import com.yisoo.common.utils.R;



/**
 * 
 *
 * @author mmciel
 * @email 761998179@qq.com
 * @date 2021-02-09 23:43:58
 */
@RestController
@RequestMapping("iot/device")
public class DeviceController {
    @Autowired
    private DeviceService deviceService;


    //仅供测试
//    @GetMapping("/open/box/test/{devsecret}")
//    public R openBoxTest(@PathVariable("devsecret") String  devSecret){
//        return deviceService.openBoxTest(devSecret);
//    }



    @GetMapping("iot/test")
    public R test(){
        System.out.println("ok");
        PubRequest request = new PubRequest();
        request.setProductKey("a1OqgLaUi5n");
        request.setMessageContent(Base64.encodeBase64String("hello world2".getBytes()));
        request.setTopicFullName("/a1OqgLaUi5n/2d3c_bag/user/get");
        request.setQos(0); //目前支持QoS0和QoS1。
        try
        {
            PubResponse response = AbstractManager.getClient().getAcsResponse(request);
            System.out.println(response.getSuccess());
            System.out.println(response.getErrorMessage());
        } catch (ClientException e)
        {
            e.printStackTrace();
        }
        return R.ok();
    }


    @RequestMapping("/detail/{did}")
    //@RequiresPermissions("iot:device:list")
    public R detail(@PathVariable("did")Long did){

        return deviceService.detail(did);
    }


//    刷新设备最新信息的接口
    @RequestMapping("/reload/{cid}")
    //@RequiresPermissions("iot:device:list")
    public R reload(@PathVariable("cid")Long cid){

        return deviceService.reload(cid);
    }

    @RequestMapping("/updstatus/{cid}")
    //@RequiresPermissions("iot:device:list")
    public R updateStatus(@PathVariable("cid")Long cid){

        return deviceService.updateStatus(cid);
    }


    @RequestMapping("/add/{cid}")
    //@RequiresPermissions("iot:device:list")
    public R addDev(@PathVariable("cid")Long cid, @RequestBody List<DevSimpleDataTo> datas){

        return deviceService.batchAdd(cid,datas);
    }






    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("iot:device:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = deviceService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{devId}")
    //@RequiresPermissions("iot:device:info")
    public R info(@PathVariable("devId") Long devId){
		DeviceEntity device = deviceService.getById(devId);

        return R.ok().put("device", device);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("iot:device:save")
    public R save(@RequestBody DeviceEntity device){
		deviceService.save(device);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("iot:device:update")
    public R update(@RequestBody DeviceEntity device){
		deviceService.updateById(device);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("iot:device:delete")
    public R delete(@RequestBody Long[] devIds){
		deviceService.removeByIds(Arrays.asList(devIds));

        return R.ok();
    }

}
